# -*- coding: utf-8 -*-
# @Author: Wang Hongbin
# @Email:   wanghongbin@ngoos.org
# @Date:   2018-03-16 14:19:27
# @Last Modified by:   Wang Hongbin
# @Last Modified time: 2018-03-28 16:36:25
import requests 
import re 
import os
import sys
import time #时间模块

reload(sys)
sys.setdefaultencoding('utf8')
local = time.strftime("%Y-%m-%d_")
baseUrl = "https://cn.bing.com"
header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36",
        "Accept-Language":"h-CN,zh;q=0.8","Content-Type":"text/xml"}

def getParentContent(url):  
    con = requests.get(url)
    content = con.text
    content = content.decode("utf-8")
    return content  

def getChildUrl(html):
    FORM = "BEHPTB"
    currentDate = time.strftime("%Y%m%d")
    IID = re.findall("<div id=\"lap_w\" data-ajaxiid=\"(.*)\" data-date=\"",html)[0]
    IG = re.findall("IG:\"(.*)\",EventID:",html)[0]
    params = "FORM="+FORM+"&currentDate="+currentDate+"&IID=SERP."+IID+"&IG="+IG
    childUrl = "https://cn.bing.com/cnhp/life?" + params
    return childUrl

def getFilePath():
	filePath = '/var/www/html/py/biYinPic/images/' + time.strftime("%Y%m%d") + '/'
	if not os.path.exists(filePath):
		os.mkdir(filePath)

	return filePath

def getTextContent(url):
	# reg = r'<div class=\"hplaCata\"><div class=\"hplatt\">(.*)</div><div class=\"hplats\">(.*)</div><div id=\"hplaSnippet\">(.*)</div><div class=\"hplaPvd\">(.*)</div>'
	reg = r"<div id=\"hplaSnippet\">(.*?)</div><div class=\"hplaPvd\">(.*?)</div>"
	con = requests.get(url)
	content = con.text
	htmlReg = re.compile(reg)
	resultList= re.findall(htmlReg,content)
	return resultList  

def writeLmk(fileName,resultList):
	'''
		{
		"firstName": "Json",
		"lastName": "Wang",
		}
	'''
	fp = open(fileName,'w+')
	fp.write("{"+'\n')
	# for i in range(len(resultList)):
	fp.write(
		'"time": "' + time.strftime("%Y-%m-%d %H:%M:%S") + '",\n' + 
		'"tstamp": "' + time.strftime("%s") + '",\n' + 
		'"arrange": "wanghongbin",\n' + 
		'"email": "wanghongbin@ngoos.org",\n' + 
		'"from": "' + str(resultList[1]) + '",\n' + 
		'"story": "'+str(resultList[0]) + '\"\n' 
		)
	
	fp.write("}")
	fp.close()

def downloadByText(url):
	pHtml = getParentContent(url)
	childUrl = getChildUrl(pHtml)
	resultList = getTextContent(childUrl)[0]
	filePath = getFilePath()
	fileName = filePath+'BiYingIntroduce.json'

	writeLmk(fileName,resultList)
	print('is ok!')

downloadByText(baseUrl)